using SimpleJSON;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class TextProgressHelper : NodeHelper
    {
        [SelectRef(UIKey.DATA_KEY)] public SelectKey maxKey;
        public Color color;
        public Color maxColor;
        private Text _Text;
        private int number = -1;

        private Text Text
        {
            get
            {
                if (_Text == null) _Text = GetComponent<Text>();
                return _Text;
            }
        }

        protected override BindDataType bindDataType => BindDataType.All;

        protected override void BindData(JSONObject obj)
        {
            var num = dataKey.Get(obj);
            var max = maxKey.Get(obj);
            var _out = num > max;
            Text.text = string.Format("{0}/{1}", _out ? max : num, max);
            if (_out) Text.color = maxColor;
            if (!_out) Text.color = color;
            number = num;
        }
    }
}